﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using System.Linq;
using Framework.Library.Core.Utils.Text;
using Framework.Library.Core.Config;

namespace Framework.Portal.Layouts.Client.Components.Teenio
{
    public partial class Write : BaseControl
    {
        public string MENUNAME = string.Empty;

        public Write()
        {
        }

        protected List<TeenioCategory> lstCategory = new List<TeenioCategory>();
        public Write(JSection section)
        {
            MENUNAME = section.title;
        }

        protected string uploadPath = string.Empty;
        protected string extension_imagelist = string.Empty;
        protected string extension_audiolist = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                uploadPath = cfg.UploadDir.VALUE + Session.GetUser().USERID + "/" + Configurations.PORTALID + "/";
                extension_imagelist = cfg.MediaFilters("image").VALUE;
                extension_imagelist = "*." + extension_imagelist.Replace(",", ";*.").Trim();
                extension_audiolist = cfg.MediaFilters("media").VALUE;
                extension_audiolist = "*." + extension_audiolist.Replace(",", ";*.").Trim();

                List<int> lstcate = new List<int>() { 1,2,3,4,5,6,7,8,9,10,11 };
                lstCategory = ManagerFactory.TeenioCategoryManager.GetCategoriesByPortal(Configurations.PORTALID);
                lstCategory = lstCategory.Join(lstcate, t => t.ZONE_ID, u => u, (t, u) => t).ToList();
                LoadZones(lstCategory, drpCategory.Items);
                filepath_image.ImageUrl = ImagePath + "images/icon_anh.png";
                btnFinish.Attributes.Add("onclick", "return validatePostEntry();");

                if(string.Equals(cfg.IsEvent.VALUE, "false", StringComparison.CurrentCultureIgnoreCase))
                    pnlEvent.Visible = false;
            }
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            TeenioContent info = GetContent();
            ManagerFactory.DAOEntity.Insert(info);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtTitle.Text = string.Empty;
            txtSource.Text = string.Empty;
            txtRawContent.Value = string.Empty;
            filepath_image_hidden.Value = string.Empty;
        }

        private TeenioContent GetContent()
        {
            var info = new TeenioContent();
            info.CONTENT_HEADLINE = txtTitle.Text.Replace("&nbsp;", " ");
            info.CONTENT_TEASER = string.Empty;
            info.CONTENT_BODY = StringUtils.RemoveHtml(txtRawContent.Value);
            info.CONTENT_PREAVATAR = filepath_image_hidden.Value;
            info.CONTENT_AVATAR = filepath_image_hidden.Value;
            info.CONTENT_AVATARDESCRIPTION = string.Empty;
            if(chkSuutam.Checked)
                info.CONTENT_SOURCE = txtSource.Text;
            else
                info.CONTENT_SOURCE = string.Empty;
            info.CONTENT_METADATA = string.Empty;
            info.CONTENT_PORTALID = Configurations.PORTALID;
            info.CONTENT_CATEGORYID = Convert.ToInt32(drpCategory.SelectedValue.Split('|')[1]);
            if (drpCategory.SelectedValue.Split('|')[0] == "0")
            {
                info.CONTENT_PLAYER = string.Empty;
                info.CONTENT_TYPE = 1;
            }
            else
            {
                info.CONTENT_PLAYER = filepath_media_hidden.Value;
                info.CONTENT_TYPE = 2;
            }
            info.CONTENT_ZONEID = 0;
            info.CONTENT_CREATEDATE = DateTime.Now;
            info.CONTENT_WAITTINGDATE = DateTime.Now;
            info.CONTENT_MODIFYDDATE = DateTime.Now;
            info.CONTENT_EXPIREDATE = DateTime.MaxValue;
            int userId = currentUserId;
            info.CONTENT_CREATEUSERID = userId;
            info.CONTENT_CREATEUSERNAME = currentUserName;
            info.CONTENT_MODIFYUSERID = userId;
            info.CONTENT_APPROVEUSERID = 0;
            info.CONTENT_RANK = 1;// StaticData.ArticleRank[1];
            info.CONTENT_STATUS = (int)CMSWorkFlow.Archive;
            info.CONTENT_ISPAY = 0;
            info.CONTENT_ISCOMMENT = 1;
            info.CONTENT_UNUNICODE = UnicodeUtility.UnicodeToKoDau(info.CONTENT_HEADLINE + " " +
                                              StringUtils.RemoveHtml(info.CONTENT_TEASER + " " + info.CONTENT_BODY));            
            return info;
        }

        public static void LoadZones(List<TeenioCategory> dtRoles, ListItemCollection lstItems)
        {
            lstItems.Clear();
            if (dtRoles != null)
            {
                foreach (TeenioCategory row in dtRoles)
                {
                    var item = new ListItem(row.ZONE_NAME, row.ZONETYPE + "|" + row.ZONE_ID.ToString());
                    lstItems.Add(item);
                }
            }
        }
    }
}